<?xml version="1.0"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>SASAPI</title><script src="../../../quicknav.js" type="text/javascript"></script><script><!--
document.quicknavBasePath="../../../index-files";
//--></script><link href="../../../style.css" title="JavaDoc" rel="stylesheet" type="text/css"/><link href="../../../unnatural.css" title="Unnatural" rel="alternate stylesheet" type="text/css"/><link href="../../../overview-summary.html" title="Overview" rel="top"/><meta name="generator" content="http://www.badgers-in-foil.co.uk/projects/as2api/"/></head><body><pre><span class="lineno" id="1">     1  </span><span class="comment doc">/**
<span class="lineno" id="2">     2  </span> * com.sekati.net.File
<span class="lineno" id="3">     3  </span> * @version 1.0.3
<span class="lineno" id="4">     4  </span> * @author jason m horwitz | sekati.com
<span class="lineno" id="5">     5  </span> * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
<span class="lineno" id="6">     6  </span> * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
<span class="lineno" id="7">     7  </span> */</span>
<span class="lineno" id="8">     8  </span> 
<span class="lineno" id="9">     9  </span><span class="key">import</span> com.sekati.core.CoreObject;
<span class="lineno" id="10">    10  </span><span class="key">import</span> com.sekati.events.Event;
<span class="lineno" id="11">    11  </span><span class="key">import</span> com.sekati.events.Dispatcher;
<span class="lineno" id="12">    12  </span><span class="key">import</span> com.sekati.utils.Delegate;
<span class="lineno" id="13">    13  </span>
<span class="lineno" id="14">    14  </span><span class="key">import</span> flash.net.FileReference;
<span class="lineno" id="15">    15  </span>
<span class="lineno" id="16">    16  </span><span class="comment doc">/**
<span class="lineno" id="17">    17  </span> * File handler class for uploading and downloading files and FileReference Events.&lt;br&gt;
<span class="lineno" id="18">    18  </span> * TODO Needs thorough testing to confirm event handling works properly.
<span class="lineno" id="19">    19  </span> */</span>
<span class="lineno" id="20">    20  </span><span class="key">class</span> com.sekati.net.File <span class="key">extends</span> CoreObject {
<span class="lineno" id="21">    21  </span>
<span class="lineno" id="22">    22  </span>	<span class="key">private</span> <span class="key">var</span> _this:File;
<span class="lineno" id="23">    23  </span>	<span class="key">private</span> <span class="key">var</span> _ref:FileReference;
<span class="lineno" id="24">    24  </span>	<span class="key">private</span> <span class="key">var</span> _listener:Object;
<span class="lineno" id="25">    25  </span>	<span class="key">public</span> <span class="key">var</span> _imageType:Object = {description: <span class="str_const">"Image Files"</span>, extension: <span class="str_const">"*.jpg;*.gif;*.png"</span>};
<span class="lineno" id="26">    26  </span>	<span class="key">public</span> <span class="key">var</span> _textType:Object = {description:<span class="str_const">"Text Files"</span>, extension:<span class="str_const">"*.txt; *.rtf"</span>};
<span class="lineno" id="27">    27  </span>	<span class="key">public</span> <span class="key">var</span> _docTypes:Object = {description: <span class="str_const">"Documents"</span>, extension: <span class="str_const">"*.pdf; *.doc; *.xls"</span>};
<span class="lineno" id="28">    28  </span>	<span class="key">public</span> <span class="key">var</span> _webTypes:Object = {description: <span class="str_const">"Web Files"</span>, extension: <span class="str_const">"*.html; *.htm; *.xhtml; *.php; *.asp; *.aspx; *.cfm; *.xml; *.xsl; *.xslt; *.css; *.js; *.jsp"</span>};
<span class="lineno" id="29">    29  </span>	<span class="key">public</span> <span class="key">var</span> _flashTypes:Object = {description: <span class="str_const">"Flash Files"</span>, extension: <span class="str_const">"*.swf; *.fla; *.as; *.flp; *.flv"</span>};
<span class="lineno" id="30">    30  </span>	<span class="key">public</span> <span class="key">var</span> _audioTypes:Object = {description: <span class="str_const">"Audio Files"</span>, extension:<span class="str_const">"*.mp3, *.aiff, *.wav"</span>};
<span class="lineno" id="31">    31  </span>	<span class="key">public</span> <span class="key">var</span> _videoTypes:Object = {description: <span class="str_const">"Video Files"</span>, extension: <span class="str_const">"*.mpg; *.mpeg; *.mp4; *.mov; *.qt; *.avi; *.wmv; *.asf"</span>};
<span class="lineno" id="32">    32  </span>	<span class="key">public</span> <span class="key">var</span> _anyType:Object = {description: <span class="str_const">"All Files"</span>, extension:<span class="str_const">"*.*"</span>};
<span class="lineno" id="33">    33  </span>	<span class="key">public</span> <span class="key">var</span> _allTypes:Array = [ _imageType, _textType, _docTypes, _webTypes, _flashTypes, _audioTypes, _videoTypes, _anyType ];
<span class="lineno" id="34">    34  </span>	<span class="comment">// events to dispatch</span>
<span class="lineno" id="35">    35  </span>	<span class="key">public</span> <span class="key">var</span> onSelectEVENT:String = <span class="str_const">"FILE_onSelect"</span>;
<span class="lineno" id="36">    36  </span>	<span class="key">public</span> <span class="key">var</span> onCancelEVENT:String = <span class="str_const">"FILE_onCancel"</span>;
<span class="lineno" id="37">    37  </span>	<span class="key">public</span> <span class="key">var</span> onOpenEVENT:String = <span class="str_const">"FILE_onOpen"</span>;
<span class="lineno" id="38">    38  </span>	<span class="key">public</span> <span class="key">var</span> onProgressEVENT:String = <span class="str_const">"FILE_onProgress"</span>;
<span class="lineno" id="39">    39  </span>	<span class="key">public</span> <span class="key">var</span> onCompleteEVENT:String = <span class="str_const">"FILE_onComplete"</span>;
<span class="lineno" id="40">    40  </span>	<span class="key">public</span> <span class="key">var</span> onHTTPErrorEVENT:String = <span class="str_const">"FILE_onHTTPError"</span>;
<span class="lineno" id="41">    41  </span>	<span class="key">public</span> <span class="key">var</span> onIOErrorEVENT:String = <span class="str_const">"FILE_onIOError"</span>;
<span class="lineno" id="42">    42  </span>	<span class="key">public</span> <span class="key">var</span> onSecurityErrorEVENT:String = <span class="str_const">"FILE_onSecurityError"</span>;
<span class="lineno" id="43">    43  </span>
<span class="lineno" id="44">    44  </span>	<span class="comment doc">/**
<span class="lineno" id="45">    45  </span>	 * Constructor
<span class="lineno" id="46">    46  </span>	 */</span>
<span class="lineno" id="47">    47  </span>	<span class="key">public</span> <span class="key">function</span> File() {
<span class="lineno" id="48">    48  </span>		<span class="key">super</span>( );
<span class="lineno" id="49">    49  </span>		_this = <span class="key">this</span>;
<span class="lineno" id="50">    50  </span>		_listener = <span class="key">new</span> Object( );
<span class="lineno" id="51">    51  </span>		_ref = <span class="key">new</span> FileReference( );
<span class="lineno" id="52">    52  </span>		_ref.addListener( _listener );
<span class="lineno" id="53">    53  </span>		_listener.onSelect = Delegate.create( _this, onSelect );
<span class="lineno" id="54">    54  </span>		_listener.onCancel = Delegate.create( _this, onCancel );
<span class="lineno" id="55">    55  </span>		_listener.onOpen = Delegate.create( _this, onOpen );
<span class="lineno" id="56">    56  </span>		_listener.onProgress = Delegate.create( _this, onProgress );
<span class="lineno" id="57">    57  </span>		_listener.onComplete = Delegate.create( _this, onComplete );
<span class="lineno" id="58">    58  </span>		_listener.onHTTPError = Delegate.create( _this, onHTTPError );
<span class="lineno" id="59">    59  </span>		_listener.onIOError = Delegate.create( _this, onIOError );
<span class="lineno" id="60">    60  </span>		_listener.onSecurityError = Delegate.create( _this, onSecurityError );
<span class="lineno" id="61">    61  </span>	}
<span class="lineno" id="62">    62  </span>
<span class="lineno" id="63">    63  </span>	<span class="comment doc">/**
<span class="lineno" id="64">    64  </span>	 * prompt user to save a remote file
<span class="lineno" id="65">    65  </span>	 * @param url (String) url of remote file
<span class="lineno" id="66">    66  </span>	 * @param defaultName (String) optional default filename for remote file to be saved as.
<span class="lineno" id="67">    67  </span>	 * @return Boolean - success status of download
<span class="lineno" id="68">    68  </span>	 */</span>
<span class="lineno" id="69">    69  </span>	<span class="key">public</span> <span class="key">function</span> download(url:String, defaultFileName:String):Boolean {
<span class="lineno" id="70">    70  </span>		<span class="key">return</span> _ref.download( url, defaultFileName );
<span class="lineno" id="71">    71  </span>	}
<span class="lineno" id="72">    72  </span>
<span class="lineno" id="73">    73  </span>	<span class="comment doc">/**
<span class="lineno" id="74">    74  </span>	 * Displays a file-browsing dialog box for the user to select a file to upload.
<span class="lineno" id="75">    75  </span>	 * @param type (Array) optional array of allowed filetypes. If none is passed the _allTypes class array will be used.
<span class="lineno" id="76">    76  </span>	 * @return Boolean - true if the dialogue box was successfully displayed.
<span class="lineno" id="77">    77  </span>	 */</span>
<span class="lineno" id="78">    78  </span>	<span class="key">public</span> <span class="key">function</span> browse(type:Array):Boolean {
<span class="lineno" id="79">    79  </span>		<span class="key">if</span> (!type) type = _allTypes;
<span class="lineno" id="80">    80  </span>		<span class="key">return</span> _ref.browse( type );	
<span class="lineno" id="81">    81  </span>	}
<span class="lineno" id="82">    82  </span>
<span class="lineno" id="83">    83  </span>	<span class="comment doc">/**
<span class="lineno" id="84">    84  </span>	 * Start a file upload (100mb max is supported by the FlashPlayer).
<span class="lineno" id="85">    85  </span>	 * Note: On some browsers, URL strings are limited in length. 
<span class="lineno" id="86">    86  </span>	 * Lengths greater than 256 characters may fail on some browsers or servers.
<span class="lineno" id="87">    87  </span>	 * @param url (String) the server url to upload file to.
<span class="lineno" id="88">    88  </span>	 * @param uploadDataField (String) optional field name that precedes the file data in the upload POST. The default is "Filedata".
<span class="lineno" id="89">    89  </span>	 * @param testUpload (Boolean) optional 0byte upload test for windows Flashplayer only. The default is false.
<span class="lineno" id="90">    90  </span>	 * @return Boolean - false if upload fails for any reason.
<span class="lineno" id="91">    91  </span>	 */</span>
<span class="lineno" id="92">    92  </span>	<span class="key">public</span> <span class="key">function</span> upload(url:String, uploadDataFieldName:String, testUpload:Boolean):Boolean {
<span class="lineno" id="93">    93  </span>		<span class="key">if</span> (!uploadDataFieldName) uploadDataFieldName = <span class="str_const">"Filedata"</span>;
<span class="lineno" id="94">    94  </span>		<span class="key">if</span> (testUpload == undefined) testUpload = <span class="key">false</span>;
<span class="lineno" id="95">    95  </span>		<span class="key">return</span> _ref.upload( url, uploadDataFieldName, testUpload );
<span class="lineno" id="96">    96  </span>	}
<span class="lineno" id="97">    97  </span>
<span class="lineno" id="98">    98  </span>	<span class="comment">// FileReference getters</span>
<span class="lineno" id="99">    99  </span>	
<span class="lineno" id="100">   100  </span>	<span class="comment doc">/**
<span class="lineno" id="101">   101  </span>	 * file creation date
<span class="lineno" id="102">   102  </span>	 * @return Date
<span class="lineno" id="103">   103  </span>	 */</span>
<span class="lineno" id="104">   104  </span>	<span class="key">public</span> <span class="key">function</span> get creationDate():Date {
<span class="lineno" id="105">   105  </span>		<span class="key">return</span> _ref.creationDate;	
<span class="lineno" id="106">   106  </span>	}
<span class="lineno" id="107">   107  </span>
<span class="lineno" id="108">   108  </span>	<span class="comment doc">/**
<span class="lineno" id="109">   109  </span>	 * macintosh creator type of the file
<span class="lineno" id="110">   110  </span>	 * @return String
<span class="lineno" id="111">   111  </span>	 */</span>
<span class="lineno" id="112">   112  </span>	<span class="key">public</span> <span class="key">function</span> get creator():String {
<span class="lineno" id="113">   113  </span>		<span class="key">return</span> _ref.creator;	
<span class="lineno" id="114">   114  </span>	}
<span class="lineno" id="115">   115  </span>
<span class="lineno" id="116">   116  </span>	<span class="comment doc">/**
<span class="lineno" id="117">   117  </span>	 * date the file was last modified on local disk
<span class="lineno" id="118">   118  </span>	 * @return Date
<span class="lineno" id="119">   119  </span>	 */</span>
<span class="lineno" id="120">   120  </span>	<span class="key">public</span> <span class="key">function</span> get modificationDate():Date {
<span class="lineno" id="121">   121  </span>		<span class="key">return</span> _ref.modificationDate;	
<span class="lineno" id="122">   122  </span>	}
<span class="lineno" id="123">   123  </span>
<span class="lineno" id="124">   124  </span>	<span class="comment doc">/**
<span class="lineno" id="125">   125  </span>	 * the file name on local disk
<span class="lineno" id="126">   126  </span>	 * @return String
<span class="lineno" id="127">   127  </span>	 */</span>
<span class="lineno" id="128">   128  </span>	<span class="key">public</span> <span class="key">function</span> get name():String {
<span class="lineno" id="129">   129  </span>		<span class="key">return</span> _ref.name;	
<span class="lineno" id="130">   130  </span>	}
<span class="lineno" id="131">   131  </span>
<span class="lineno" id="132">   132  </span>	<span class="comment doc">/**
<span class="lineno" id="133">   133  </span>	 * the file size on local disk in bytes
<span class="lineno" id="134">   134  </span>	 * @return Number
<span class="lineno" id="135">   135  </span>	 */</span>
<span class="lineno" id="136">   136  </span>	<span class="key">public</span> <span class="key">function</span> get size():Number {
<span class="lineno" id="137">   137  </span>		<span class="key">return</span> _ref.size;	
<span class="lineno" id="138">   138  </span>	}
<span class="lineno" id="139">   139  </span>
<span class="lineno" id="140">   140  </span>	<span class="comment doc">/**
<span class="lineno" id="141">   141  </span>	 * the file type
<span class="lineno" id="142">   142  </span>	 * @return String
<span class="lineno" id="143">   143  </span>	 */</span>
<span class="lineno" id="144">   144  </span>	<span class="key">public</span> <span class="key">function</span> get type():String {
<span class="lineno" id="145">   145  </span>		<span class="key">return</span> _ref.type;	
<span class="lineno" id="146">   146  </span>	}
<span class="lineno" id="147">   147  </span>
<span class="lineno" id="148">   148  </span>	<span class="comment">// FileReference Event handlers	</span>
<span class="lineno" id="149">   149  </span>	<span class="key">private</span> <span class="key">function</span> onSelect(file:FileReference):Void {
<span class="lineno" id="150">   150  </span>		trace( <span class="str_const">"onSelect: "</span> + file.name );
<span class="lineno" id="151">   151  </span>		Dispatcher( <span class="key">new</span> Event( onSelectEVENT, _this, {file: file} ) );
<span class="lineno" id="152">   152  </span>		<span class="comment">/*
<span class="lineno" id="153">   153  </span>	    if(!file.upload("http://www.yourdomain.com/yourUploadHandlerScript.cfm")) {
<span class="lineno" id="154">   154  </span>	        trace("Upload dialog failed to open.");
<span class="lineno" id="155">   155  </span>	    }
<span class="lineno" id="156">   156  </span>	    */</span>
<span class="lineno" id="157">   157  </span>	}
<span class="lineno" id="158">   158  </span>
<span class="lineno" id="159">   159  </span>	<span class="key">private</span> <span class="key">function</span> onCancel(file:FileReference):Void {
<span class="lineno" id="160">   160  </span>		trace( <span class="str_const">"onCancel"</span> );
<span class="lineno" id="161">   161  </span>		Dispatcher( <span class="key">new</span> Event( onCancelEVENT, _this, {file: file} ) );
<span class="lineno" id="162">   162  </span>	}
<span class="lineno" id="163">   163  </span>
<span class="lineno" id="164">   164  </span>	<span class="key">private</span> <span class="key">function</span> onOpen(file:FileReference):Void {
<span class="lineno" id="165">   165  </span>		trace( <span class="str_const">"onOpen: "</span> + file.name );
<span class="lineno" id="166">   166  </span>		Dispatcher( <span class="key">new</span> Event( onOpenEVENT, _this, {name: file.name} ) );
<span class="lineno" id="167">   167  </span>	}
<span class="lineno" id="168">   168  </span>
<span class="lineno" id="169">   169  </span>	<span class="key">private</span> <span class="key">function</span> onProgress(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
<span class="lineno" id="170">   170  </span>		trace( <span class="str_const">"onProgress with bytesLoaded: "</span> + bytesLoaded + <span class="str_const">" bytesTotal: "</span> + bytesTotal );
<span class="lineno" id="171">   171  </span>		Dispatcher( <span class="key">new</span> Event( onProgressEVENT, _this, {bytesLoaded: bytesLoaded, bytesTotal: bytesTotal} ) );
<span class="lineno" id="172">   172  </span>	}
<span class="lineno" id="173">   173  </span>
<span class="lineno" id="174">   174  </span>	<span class="key">private</span> <span class="key">function</span> onComplete(file:FileReference):Void {
<span class="lineno" id="175">   175  </span>		trace( <span class="str_const">"onComplete: "</span> + file.name );
<span class="lineno" id="176">   176  </span>		Dispatcher( <span class="key">new</span> Event( onCompleteEVENT, _this, {file:file} ) );
<span class="lineno" id="177">   177  </span>	}
<span class="lineno" id="178">   178  </span>
<span class="lineno" id="179">   179  </span>	<span class="key">private</span> <span class="key">function</span> onHTTPError(file:FileReference):Void {
<span class="lineno" id="180">   180  </span>		trace( <span class="str_const">"onHTTPError: "</span> + file.name );
<span class="lineno" id="181">   181  </span>		Dispatcher( <span class="key">new</span> Event( onHTTPErrorEVENT, _this, {name: file.name} ) );
<span class="lineno" id="182">   182  </span>	}
<span class="lineno" id="183">   183  </span>
<span class="lineno" id="184">   184  </span>	<span class="key">private</span> <span class="key">function</span> onIOError(file:FileReference):Void {
<span class="lineno" id="185">   185  </span>		trace( <span class="str_const">"onIOError: "</span> + file.name );
<span class="lineno" id="186">   186  </span>		Dispatcher( <span class="key">new</span> Event( onIOErrorEVENT, _this, {name: file.name} ) );
<span class="lineno" id="187">   187  </span>	}	
<span class="lineno" id="188">   188  </span>
<span class="lineno" id="189">   189  </span>	<span class="key">private</span> <span class="key">function</span> onSecurityError(file:FileReference, errorString:String):Void {
<span class="lineno" id="190">   190  </span>		trace( <span class="str_const">"onSecurityError: "</span> + file.name + <span class="str_const">" errorString: "</span> + errorString );
<span class="lineno" id="191">   191  </span>		Dispatcher( <span class="key">new</span> Event( onSecurityErrorEVENT, _this, {name: file.name, errorString: errorString} ) );
<span class="lineno" id="192">   192  </span>	}
<span class="lineno" id="193">   193  </span>}</pre><ul class="main_nav" id="main_nav"><li><a href="../../../overview-summary.html" title="Overview of SASAPI" class="button">Overview</a></li><li><a href="package-summary.html" title="Overview of package com.sekati.net" class="button">Package</a></li><li><a href="File.html" title="Detail of com.sekati.net.File API" class="button">Class</a></li><li><span class="button nav_current">Source</span></li><li><a href="../../../index-files/index.html" title="Alpabetical index of types and members" class="button">Index</a></li></ul><div class="footer"><a href="http://www.badgers-in-foil.co.uk/projects/as2api/" title="ActionScript 2 API Documentation Generator">as2api</a></div></body></html>